import 'package:flutter/material.dart';

class HouseRentSaleRatioScreen extends StatefulWidget {
  const HouseRentSaleRatioScreen({super.key});

  @override
  State<HouseRentSaleRatioScreen> createState() =>
      _HouseRentSaleRatioScreenState();
}

class _HouseRentSaleRatioScreenState extends State<HouseRentSaleRatioScreen> {
  final TextEditingController _rentController = TextEditingController();
  final TextEditingController _minRatioController = TextEditingController();
  final TextEditingController _priceController = TextEditingController();
  final TextEditingController _areaController =
      TextEditingController(text: '90');

  String _priceRangeResult = '';
  String _unitPriceRangeResult = '';
  String _ratioResult = '';

  void _calculatePriceRange() {
    final rent = double.tryParse(_rentController.text);
    final minRatio = double.tryParse(_minRatioController.text);
    final area = double.tryParse(_areaController.text);
    if (rent != null &&
        minRatio != null &&
        minRatio > 0 &&
        area != null &&
        area > 0) {
      final minPrice = rent * 12 / (minRatio / 100);
      final minUnitPrice = minPrice / area;
      setState(() {
        _priceRangeResult =
            '合理房价：${minPrice.toStringAsFixed(0)}元';
        _unitPriceRangeResult =
            '对应单价：${minUnitPrice.toStringAsFixed(0)}元/平米';
      });
    } else {
      setState(() {
        _priceRangeResult = '请输入有效的房租、租售比和面积';
        _unitPriceRangeResult = '';
      });
    }
  }

  void _calculateRatio() {
    final rent = double.tryParse(_rentController.text);
    final price = double.tryParse(_priceController.text);
    final area = double.tryParse(_areaController.text);
    if (rent != null &&
        price != null &&
        price > 0 &&
        area != null &&
        area > 0) {
      final ratio = rent * 12 / (price * area) * 100;
      setState(() {
        _ratioResult = '房屋总价：${price * area}元，租售比：${ratio.toStringAsFixed(2)}%';
      });
    } else {
      setState(() {
        _ratioResult = '请输入有效的房租和房价';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('房屋租售比计算'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          const Text('通过房租和租售比计算房价区间',
              style: TextStyle(fontWeight: FontWeight.bold)),
          TextField(
            controller: _areaController,
            keyboardType: TextInputType.number,
            decoration: const InputDecoration(labelText: '房屋面积（平米）'),
          ),
          TextField(
            controller: _rentController,
            keyboardType: TextInputType.number,
            decoration: const InputDecoration(labelText: '月房租（元）'),
          ),
          Row(
            children: [
              Expanded(
                child: TextField(
                  controller: _minRatioController,
                  keyboardType: TextInputType.number,
                  decoration: const InputDecoration(labelText: '租售比（%）'),
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          ElevatedButton(
            onPressed: _calculatePriceRange,
            child: const Text('计算房价'),
          ),
          Text(_priceRangeResult, style: const TextStyle(color: Colors.blue)),
          Text(_unitPriceRangeResult,
              style: const TextStyle(color: Colors.orange)),
          const Divider(height: 32),
          const Text('通过房租和房价反算租售比',
              style: TextStyle(fontWeight: FontWeight.bold)),
          TextField(
            controller: _priceController,
            keyboardType: TextInputType.number,
            decoration: const InputDecoration(labelText: '房价单价（元/平米）'),
          ),
          const SizedBox(height: 8),
          ElevatedButton(
            onPressed: _calculateRatio,
            child: const Text('计算租售比'),
          ),
          Text(_ratioResult, style: const TextStyle(color: Colors.green)),
        ],
      ),
    );
  }
}
